package aaa.schedule;

import java.util.ArrayList;

import aaa.database.Database;
import aaa.exception.AAAException;
import android.content.Context;

public abstract class ScheduleUpdater {

	protected ArrayList<Lesson> insert ;

	protected ArrayList<Lesson> update ;

	protected ArrayList<Lesson> delete ;

	protected Database db ;

	/**
	 * 
	 */
	protected ScheduleUpdater(Context context) {
		super() ;
		this.db = new Database(context) ;
	}// ScheduleUpdater()
	
	protected void initialize () {
		this.insert = new ArrayList<Lesson>() ;
		this.update = new ArrayList<Lesson>() ;
		this.delete = new ArrayList<Lesson>() ;
	}// initialize()
	
	/**
	 * 
	 * @param oldS
	 * @param newS
	 */
	public void compare (Schedule oldS, Schedule newS) {
		this.initialize() ;
		this.compareS(oldS, newS) ;
	}// compare(Schedule, Schedule) 
	
	/**
	 * 
	 * @param oldS
	 * @param newS
	 */
	protected abstract void compareS (Schedule oldS, Schedule newS) ;
	
	public void insert () throws AAAException {
		if (this.insert.size() == 0) {
			return ; // No rows to insert
		}// if
		this.db.insertLessons(this.insert) ;
	}// insert()
	
	public void update () throws AAAException  {
		if (this.update.size() == 0) {
			return ; // No rows to update
		}// if
		this.db.updateLessons(this.update) ;
	}// update()
	
	public void delete () throws AAAException  {
		if (this.delete.size() == 0) {
			return ; // No rows to delete
		}// if
		this.db.deleteLessons(this.delete, this.delete.get(0).getSchedule()) ;
	}// delete()
	
	public void updateSchedule () throws AAAException {
		this.delete() ;
		this.update() ;
		this.insert() ;
	}// updateSchedule()
	
}
